<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>AI图片编辑</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            max-width: 800px;
            margin: 0 auto;
            padding: 20px;
            background-color: #f5f5f5;
        }
        .container {
            background-color: white;
            padding: 30px;
            border-radius: 10px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
        }
        h1 {
            color: #333;
            text-align: center;
        }
        .form-group {
            margin-bottom: 20px;
        }
        label {
            display: block;
            margin-bottom: 5px;
            font-weight: bold;
        }
        input[type="text"], textarea, input[type="file"] {
            width: 100%;
            padding: 10px;
            border: 1px solid #ddd;
            border-radius: 5px;
            box-sizing: border-box;
        }
        textarea {
            height: 100px;
            resize: vertical;
        }
        button {
            background-color: #007bff;
            color: white;
            padding: 12px 30px;
            border: none;
            border-radius: 5px;
            cursor: pointer;
            font-size: 16px;
            width: 100%;
        }
        button:hover {
            background-color: #0056b3;
        }
        .result {
            margin-top: 20px;
            padding: 15px;
            border-radius: 5px;
            display: none;
        }
        .success {
            background-color: #d4edda;
            border: 1px solid #c3e6cb;
            color: #155724;
        }
        .error {
            background-color: #f8d7da;
            border: 1px solid #f5c6cb;
            color: #721c24;
        }
        .back-link {
            display: inline-block;
            margin-top: 20px;
            color: #007bff;
            text-decoration: none;
        }
        .back-link:hover {
            text-decoration: underline;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>AI图片编辑</h1>
        <form id="editForm" enctype="multipart/form-data">
            <div class="form-group">
                <label for="image_file">选择图片:</label>
                <input type="file" id="image_file" name="image_file" accept="image/*" required>
            </div>
            
            <div class="form-group">
                <label for="prompt">编辑描述:</label>
                <textarea id="prompt" name="prompt" placeholder="请输入图片编辑的描述，例如：将自行车改为红色，背景改为海滩" required>{{ cached_inputs.prompt or '' }}</textarea>
            </div>
            
            <div class="form-group">
                <label for="negative_prompt">负面描述 (可选):</label>
                <input type="text" id="negative_prompt" name="negative_prompt" placeholder="不希望出现在图片中的元素" value="{{ cached_inputs.negative_prompt or '' }}">
            </div>
            
            <button type="submit">编辑图片</button>
        </form>
        
        <div id="result" class="result"></div>
        
        <a href="{{ url_for('index') }}" class="back-link">← 返回主页</a>
    </div>

    <script>
        document.getElementById('editForm').addEventListener('submit', function(e) {
            e.preventDefault();
            
            const formData = new FormData(this);
            const resultDiv = document.getElementById('result');
            
            // 显示处理中消息
            resultDiv.className = 'result';
            resultDiv.innerHTML = '<p>正在处理图片，请稍候...</p>';
            resultDiv.style.display = 'block';
            
            // 禁用提交按钮
            const submitButton = this.querySelector('button');
            submitButton.disabled = true;
            submitButton.textContent = '处理中...';
            
            // 发送请求
            fetch('/image-edit-process', {
                method: 'POST',
                body: formData
            })
            .then(response => response.json())
            .then(data => {
                if (data.success) {
                    // 重定向到任务状态页面
                    window.location.href = '/image-edit-task/' + data.task_id;
                } else {
                    resultDiv.className = 'result error';
                    resultDiv.innerHTML = `<h3>处理失败</h3><p>${data.error}</p>`;
                    submitButton.disabled = false;
                    submitButton.textContent = '编辑图片';
                }
            })
            .catch(error => {
                resultDiv.className = 'result error';
                resultDiv.innerHTML = `<h3>处理失败</h3><p>${error.message}</p>`;
                submitButton.disabled = false;
                submitButton.textContent = '编辑图片';
            });
        });
    </script>
</body>
</html>